package org.dromara.bean.manage;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import lombok.RequiredArgsConstructor;
import org.dromara.bean.constant.CommonCode;
import org.dromara.bean.domain.HisMember;
import org.dromara.bean.domain.HisMemberRefund;
import org.dromara.bean.domain.HisRegistration;
import org.dromara.bean.domain.vo.HisGoodsOrderVo;
import org.dromara.bean.domain.vo.HisMemberVo;
import org.dromara.bean.domain.vo.HisRegistrationVo;
import org.dromara.bean.mapper.BaseMemberRefundMapper;
import org.dromara.bean.mapper.HisGoodsOrderMapper;
import org.dromara.bean.mapper.HisRegistrationMapper;
import org.dromara.bean.service.IBaseMemberRefundService;
import org.dromara.bean.service.IHisGoodsOrderService;
import org.dromara.bean.service.IHisMemberService;
import org.dromara.bean.service.IHisRegistrationService;
import org.dromara.bean.util.wxApi.WXHandleUtil;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.WxRefundResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Description: 患者退费业务层
 *
 * @author Handy
 * Date: 2023/12/7
 */
@Component
@RequiredArgsConstructor
public class MemberRefundManage {
    private static final String CHECK_STATUS_APPROVE = "1";
    private static final String CHECK_STATUS_REFUSE = "-1";
    private static final String CHECK_STATUS_WAIT_APPROVE = "0";

    private final HisGoodsOrderMapper goodsOrderMapper;
    private final HisRegistrationMapper hisRegistrationMapper;

    private final IHisMemberService memberService;

    private final BaseMemberRefundMapper baseMemberRefundMapper;
    private final WXHandleUtil wxHandleUtil;

    /*
     *
     * 商城订单审核
     *
     * */
    public R<?> audit4Commodity(HisMemberRefund memberRefund, String checkStatus, String cause) {
        HisGoodsOrderVo hisGoodsOrderVo = goodsOrderMapper.selectVoById(memberRefund.getOrderTreatId());

        if (Objects.isNull(hisGoodsOrderVo)) {
            return R.error(500, "该订单已不存在!请核实相关信息!");
        }
        if (CHECK_STATUS_WAIT_APPROVE.equals(checkStatus)) {
            return R.error(500, "请勿设置为待审核状态");
        }
        memberRefund.setUpdateTime(DateUtil.date());
        memberRefund.setCheckStatus(checkStatus);
        memberRefund.setRefuseCause(cause);
        HisMemberVo member = memberService.queryById(memberRefund.getMemberId());
        if (CHECK_STATUS_REFUSE.equals(checkStatus) && !CHECK_STATUS_APPROVE.equals(memberRefund.getCheckStatus())) {
            if (baseMemberRefundMapper.updateById(memberRefund) > 0) {

                return R.ok(CommonCode.SMART_HOSPITAL_SUCCESS);
            } else {
                return R.errorCode(CommonCode.UPDATE_FAIL);
            }
        } else if (CHECK_STATUS_APPROVE.equals(checkStatus)) {
            WxRefundResponseDTO dto = wxHandleUtil.refundCommidityExamine(memberRefund.getId());
            if (dto.isSuccess()) {
                memberRefund.setStatus("2");
                baseMemberRefundMapper.updateById(memberRefund);

                return R.ok(CommonCode.SMART_HOSPITAL_SUCCESS);
            } else {
                return R.error(500, "审核通过订单失败： " + dto.getMessage());
            }
        } else {
            return R.error(500, "不支持的审核状态");
        }
    }

    /*
     *
     * 挂号订单审核
     *
     * */
    public R<?> audit4Registration(HisMemberRefund memberRefund, String checkStatus, String cause) {
        HisRegistrationVo registrationVo = hisRegistrationMapper.selectVoById(memberRefund.getOrderTreatId());

        if (Objects.isNull(registrationVo)) {
            return R.error(500, "该订单已不存在!请核实相关信息!");
        }
        if (CHECK_STATUS_WAIT_APPROVE.equals(checkStatus)) {
            return R.error(500, "请勿设置为待审核状态");
        }
        memberRefund.setUpdateTime(DateUtil.date());
        memberRefund.setCheckStatus(checkStatus);
        memberRefund.setRefuseCause(cause);
        HisMemberVo member = memberService.queryById(memberRefund.getMemberId());
        if (CHECK_STATUS_REFUSE.equals(checkStatus) && !CHECK_STATUS_APPROVE.equals(memberRefund.getCheckStatus())) {
            if (baseMemberRefundMapper.updateById(memberRefund) > 0) {

                return R.ok(CommonCode.SMART_HOSPITAL_SUCCESS);
            } else {
                return R.errorCode(CommonCode.UPDATE_FAIL);
            }
        } else if (CHECK_STATUS_APPROVE.equals(checkStatus)) {
            WxRefundResponseDTO dto = wxHandleUtil.refundCommidityExamine(memberRefund.getId());
            if (dto.isSuccess()) {
                memberRefund.setStatus("2");
                baseMemberRefundMapper.updateById(memberRefund);

                return R.ok(CommonCode.SMART_HOSPITAL_SUCCESS);
            } else {
                return R.error(500, "审核通过订单失败： " + dto.getMessage());
            }
        } else {
            return R.error(500, "不支持的审核状态");
        }
    }




}
